'use strict'

class posts {

  constructor () {
    //param默认就是/:id，若要改变路由，则可以设置此属性，比如设置为/:name
    //this.param = '/:id'
  }

  //以下方法，若不需要，要去掉，避免无意义的路由。

  //获取资源具体内容
  // async get(c) {
  //   let sqltext = `SELECT * FROM square WHERE posts_id = $1`;
  //   let r = await c.service.psql.query(sqltext, [c.param.id]);
  //   if (r.rowCount <= 0) return c.status(404).send('not found');
  //   c.send(JSON.stringify(r.rows[0]));
  // }

  async get(c) {
    let sqltext = `SELECT * FROM square LEFT JOIN users on square.open_id=users.open_id WHERE square.posts_id = $1`;
    let r = await c.service.psql.query(sqltext, [c.param.id]);
    if (r.rowCount <= 0) return c.status(404).send('not found');
    c.send(JSON.stringify(r.rows[0]));
  }

  async list(c) {
    let sqltext = `SELECT * FROM square left join users on square.open_id=users.open_id LIMIT 3 OFFSET 1`;
    let r = await c.service.psql.query(sqltext);
    c.send(JSON.stringify(r.rows));
  };
}

  module.exports = posts;